package com.wash.shoes.controller.mobile;


import com.ruoyi.common.core.domain.R;
import com.wash.shoes.entity.CustomerPo;
import com.wash.shoes.service.CustomerService;
import com.wash.shoes.util.WsRandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ljg
 * @since 2024-08-28
 */
@Api(tags = {"移动端-客户"})
@RestController
@RequestMapping("/mobile-api/customer")
public class CustomerMController {

    @Autowired
    CustomerService customerService;

    @ApiOperation("修改客户信息,有id为修改没有为新增")
    @PostMapping("/saveUpdate")
    public R<CustomerPo> saveUpdate(@RequestBody CustomerPo customerPo) {
        CustomerPo customerPoDb = customerService.getById(customerPo.getId());
        customerPoDb.setHeadUrl(customerPo.getHeadUrl());
        customerPoDb.setName(customerPo.getName());
        customerService.updateById(customerPoDb);

        return R.ok(customerPo);
    }

    @ApiOperation("获取详情")
    @PostMapping("/getDetailById")
    public R<CustomerPo> getDetailById(String theId) {
        CustomerPo customerPo = customerService.getById(theId);
        // 获取邀请码
        String code = WsRandomUtil.generateByRandom(10);
        CustomerPo customerPoDb = customerService.getByInviteCode(code);
        while (customerPoDb != null) {
            code = WsRandomUtil.generateByRandom(10);
            customerPoDb = customerService.getByInviteCode(code);
        }
        customerPo.setInviteCode(code);
        customerService.updateById(customerPo);
        return R.ok(customerPo);
    }

}

